function  [Total_P_decision_j,Total_P_experience_j,Total_SW,Total_SW_experience,Total_W_avg,Total_ext_avg,Total_gvt_avg]=Total_Welfare_clogit_EEgap_by_j(weight,param,param_FE,param_FEdemo,id_ij,J,p_elec,t_pigou,d_ext,elec,lifetime,r,MCPF)

global state_i week_i T S MT MTvector K WB...
price price_denom rebate_estar_denom estar_denom kwh prod_denom...
id_i id_j id_m id_n  demo pidnest choicenest_ij brandweek_denom ...
type_id2_3_demo1_num type_id2_3_demo1_denom type_id3_demo1_num type_id3_demo1_denom AV_demo1_num AV_demo1_denom  ice_sc_demo1_num ice_sc_demo1_denom...
type_id2_3_demo2_num type_id2_3_demo2_denom type_id3_demo2_num type_id3_demo2_denom AV_demo2_num AV_demo2_denom  ice_sc_demo2_num ice_sc_demo2_denom...
type_id2_3_demo3_num type_id2_3_demo3_denom type_id3_demo3_num type_id3_demo3_denom AV_demo3_num AV_demo3_denom  ice_sc_demo3_num ice_sc_demo3_denom...
type_id2_3_demo4_num type_id2_3_demo4_denom type_id3_demo4_num type_id3_demo4_denom AV_demo4_num AV_demo4_denom  ice_sc_demo4_num ice_sc_demo4_denom...
type_id2_3_demo5_num type_id2_3_demo5_denom type_id3_demo5_num type_id3_demo5_denom AV_demo5_num AV_demo5_denom  ice_sc_demo5_num ice_sc_demo5_denom...
type_id2_3_demo6_num type_id2_3_demo6_denom type_id3_demo6_num type_id3_demo6_denom AV_demo6_num AV_demo6_denom  ice_sc_demo6_num ice_sc_demo6_denom;


    param_FE_k=param_FE;
	param_FEdemo_k=param_FEdemo;

%%	
%  Loop over the k types and compute choice probabilities and various welfare metrics
	
dim_k=length(weight)
%parfor k=1:dim_k
for k=1:dim_k
k
	param_k=param{k};

[Error_i{k}, CS_i{k}, W_i{k}, W_experience_i{k},  Error_avg(k), CS_avg(k), W_avg(k), W_experience_avg(k), ext_i{k}, ext_avg(k), ext_experience_i{k}, ext_experience_avg(k), P_decision_j{k}, P_experience_j{k},gvt_i{k},gvt_avg(k),SW_i{k},SW_avg(k),gvt_experience_i{k},gvt_experience_avg(k),SW_experience_i{k},SW_experience_avg(k)]=Welfare_k_clogit_EEgap(param_k,param_FE_k,param_FEdemo_k,id_ij,J,p_elec,t_pigou,d_ext,elec,lifetime,r,MCPF);

 P_decision_j_tmp(:,k)=P_decision_j{k}*weight(k);
 P_experience_j_tmp(:,k)=P_experience_j{k}*weight(k);

end

%%
%  Weight and sum each type to compute the average over the k types

%   Choice probabilities of the decision model, this what we shall use to simulate the market
    Total_P_decision_j=sum(P_decision_j_tmp,2);
    
%   Choice probabilities of the experienced utility model
%    Those are the choice prob. if we force consumers to be rational.
    Total_P_experience_j=sum(P_experience_j_tmp,2);
    
%   Social welfare: W+Gvt-Ext, where W accounts for consumers' misperceptions  
	Total_SW=sum(weight'.*SW_avg);
	
%   Social welfare is we exlude misperceptions: W_experience+Gvt-Ext, where W accounts for consumers' misperceptions  	
	Total_SW_experience=sum(weight'.*SW_experience_avg);
	
%   Consumer surplus with mispercetions 	
	Total_W_avg=sum(weight'.*W_avg);
	
%   Externatility costs	
	Total_ext_avg=sum(weight'.*ext_avg);
	
%   Government revenues if a Pigouvian tax is levied	
	Total_gvt_avg=sum(weight'.*gvt_avg);
	
 
end